package framework.view {
	import Box2D.Dynamics.b2Body;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import framework.signals.CreateBodySignal;
	import framework.signals.UpdateWorldSignal;
	import framework.signals.WorldUpdatedSignal;
	import org.osflash.signals.natives.NativeSignal;

	/**
	 * @author Charlie MacIsaac
	 */
	public class WorldCanvasMediator extends BlitCanvasMediator {
		// PROPERTIES =============================================================================
		[Inject]	public var createBody : CreateBodySignal;
		[Inject]	public var updateWorld : UpdateWorldSignal;
		[Inject]	public var worldUpdated : WorldUpdatedSignal;
		
		private var _stageClickSignal : NativeSignal;
		private var _viewEnterFrameSignal : NativeSignal;
		
		// GETTERS + SETTERS ======================================================================
		// CONSTRUCTOR ============================================================================
		public function WorldCanvasMediator() { }
		
		// PUBLIC =================================================================================
		override public function onRegister() : void {
			super.onRegister();
			
			_stageClickSignal = new NativeSignal(view.stage, MouseEvent.CLICK, MouseEvent);
			_viewEnterFrameSignal = new NativeSignal(view, Event.ENTER_FRAME, Event);
			
			_stageClickSignal.add(viewClicked);
			_viewEnterFrameSignal.add(triggerWorldUpdate);
			worldUpdated.add(renderWorld);
		}
		
		// PRIVATE ================================================================================
		// LISTENERS ==============================================================================
		private function triggerWorldUpdate(e: Event) : void {
			updateWorld.dispatch();
		}
		
		private function viewClicked(e : MouseEvent) : void {
			createBody.dispatch();
		}
		
		private function renderWorld(bodies : Vector.<b2Body>) : void {
			view.render();
		}
	}
}